﻿Public Class 结算窗体

    '窗体加载
    Private Sub 结算窗体_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupBox3.Visible = False
        Me.Height = 224
        Button2.Enabled = False

        '加载品牌
        Dim sqlstr As String = "select 品牌 from 品牌表"
        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        If ds_table.Rows.Count > 0 Then
            For i = 0 To ds_table.Rows.Count - 1
                ComboBox1.Items.Add(ds_table.Rows(i).Item(0).ToString)
            Next
        End If

        DateTimePicker1.Value = DateTimePicker1.Value.AddMonths(-1)
        DateTimePicker3.Value = DateTimePicker3.Value.AddMonths(-1)

    End Sub

    '执行结算
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        If CheckBox1.Checked Then
            If ComboBox1.Text = "" Then MsgBox("请选择品牌！") : ComboBox1.Focus() : Exit Sub
        End If
        If CheckBox2.Checked Then
            If ComboBox2.Text = "" Then MsgBox("请选择型号！") : ComboBox2.Focus() : Exit Sub
        End If

        Me.Height = 457
        GroupBox3.Visible = True
        Button2.Enabled = True

        '（1）入货记录搜索
        '（1.1）sql构造
        Dim d1 As Date = DateTimePicker1.Value
        Dim d2 As Date = DateTimePicker2.Value
        d2.AddDays(1)
        Dim sql_RH As String = "select * from 入货表 where "
        sql_RH += "日期 >= #" + d1 + "# and 日期 < #" + d2 + "#"

        If CheckBox1.Checked Then
            If ComboBox1.Text = "" Then MsgBox("请选择品牌！") : ComboBox1.Focus() : Exit Sub
            sql_RH += " and 品牌 = '" + ComboBox1.Text + "'"
        End If
        If CheckBox2.Checked Then
            If ComboBox2.Text = "" Then MsgBox("请选择型号！") : ComboBox2.Focus() : Exit Sub
            sql_RH += " and 型号 = '" + ComboBox2.Text + "'"
        End If

        '（1.2）执行sql
        Dim ds_RH As DataTable = SQLtoDataSet(sql_RH).Tables(0)
        If ds_RH.Rows.Count > 0 Then
            Dim XL As Integer = 0
            Dim ZC As Integer = 0
            For i = 0 To ds_RH.Rows.Count - 1
                XL += ds_RH.Rows(i).Item("数量")
                ZC += ds_RH.Rows(i).Item("合计总额")
            Next
            Label18.Text = XL.ToString + " 台"
            Label18.Tag = XL.ToString
            Label20.Text = ZC.ToString + " 元"
            Label20.Tag = ZC.ToString
            Label19.Text = ""
            If CheckBox1.Checked Then Label19.Text += ComboBox1.Text
            If CheckBox2.Checked Then Label19.Text += " " + ComboBox2.Text
            If Not CheckBox1.Checked And Not CheckBox2.Checked Then Label19.Text = "多种型号！"
        Else
            Label19.Text = "没有记录！"
            Label18.Text = "没有记录！"
            Label20.Text = "没有记录！"
        End If



        '（2）出售记录搜索
        '（2.1）sql构造
        Dim d3 As Date = DateTimePicker1.Value
        Dim d4 As Date = DateTimePicker2.Value
        d4.AddDays(1)
        Dim sql_CS As String = "select * from 出售表 where "
        sql_CS += "日期 >= #" + d1 + "# and 日期 < #" + d4 + "#"

        If CheckBox1.Checked Then
            If ComboBox1.Text = "" Then MsgBox("请选择品牌！") : ComboBox1.Focus() : Exit Sub
            sql_CS += " and 品牌 = '" + ComboBox1.Text + "'"
        End If
        If CheckBox2.Checked Then
            If ComboBox2.Text = "" Then MsgBox("请选择型号！") : ComboBox2.Focus() : Exit Sub
            sql_CS += " and 型号 = '" + ComboBox2.Text + "'"
        End If

        '（1.2）执行sql
        Dim ds_CS As DataTable = SQLtoDataSet(sql_CS).Tables(0)
        If ds_CS.Rows.Count > 0 Then
            Dim XL As Integer = 0
            Dim SR As Integer = 0
            For i = 0 To ds_CS.Rows.Count - 1
                XL += ds_CS.Rows(i).Item("售出数量")
                SR += ds_CS.Rows(i).Item("出售总额")
            Next
            Label21.Text = XL.ToString + " 台"
            Label21.Tag = XL.ToString
            Label23.Text = SR.ToString + " 元"
            Label23.Tag = SR.ToString
            Label22.Text = ""
            If CheckBox1.Checked Then Label22.Text += ComboBox1.Text
            If CheckBox2.Checked Then Label22.Text += " " + ComboBox2.Text
            If Not CheckBox1.Checked And Not CheckBox2.Checked Then Label22.Text = "多种型号！"
        Else
            Label21.Text = "没有记录！"
            Label22.Text = "没有记录！"
            Label23.Text = "没有记录！"
        End If

        If ds_RH.Rows.Count = 0 And ds_CS.Rows.Count = 0 Then
            GroupBox3.Visible = False
            Button2.Enabled = False
            Me.Height = 224
            MsgBox("没有出入货记录！")
            Exit Sub
        End If


        If Label21.Text = "没有记录！" And Label18.Text <> "没有记录！ Then" Then
            Label24.Text = (Val(Label18.Tag.ToString) - 0).ToString + " 台"
            Label27.Text = Label20.Tag.ToString + " 元"
            Label26.Text = "0 元"
            Label25.Text = Label27.Text
            Label28.Text = "0 元"
        ElseIf Label18.Text = "没有记录！" Then
            Label24.Text = "没有入货记录！"
            Label27.Text = "没有入货记录！"
            Label26.Text = "没有入货记录！"
            Label25.Text = "没有入货记录！"
            Label28.Text = "没有入货记录！"
        Else
            Dim rest As Integer = Val(Label18.Tag.ToString) - Val(Label21.Tag.ToString)

            Label24.Text = rest.ToString + " 台"
            Label27.Text = Label20.Tag.ToString + " 元"
            Label26.Text = Label23.Tag.ToString + " 元"

            Dim aa As Integer = 0
            Dim DSZ As Integer = 0
            For i = 0 To ds_RH.Rows.Count - 1
                Dim cc As Integer = ds_RH.Rows(i).Item("数量")
                While cc > 0
                    DSZ += ds_RH.Rows(i).Item("入货单价")
                    cc -= 1
                    aa += 1
                    If aa = rest Then Exit For
                End While

            Next

            Label25.Text = DSZ.ToString + " 元"
            Label28.Text = (Val(Label23.Tag.ToString) + DSZ - Val(Label20.Tag.ToString)).ToString + " 元"

        End If

    End Sub

#Region "外观+控件"

    '关闭窗体
    Private Sub 结算窗体_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        CalulaForm = Nothing
    End Sub

    '选择品牌
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        '加载型号
        Dim sqlstr As String = "select 型号 from 型号表 where 所属品牌 = '" + ComboBox1.Text + "'"
        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        ComboBox2.Items.Clear()
        If ds_table.Rows.Count > 0 Then
            For i = 0 To ds_table.Rows.Count - 1
                ComboBox2.Items.Add(ds_table.Rows(i).Item(0).ToString)
            Next
        End If

    End Sub
    Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus
        ComboBox1_SelectedIndexChanged(sender, e)
    End Sub

    '品牌必选
    Private Sub CheckBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.Click
        CheckBox1.Checked = True
    End Sub

    '型号必选
    Private Sub CheckBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox2.Click
        CheckBox2.Checked = True
    End Sub

#End Region

    '打印按扭
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        '强制横向打印
        PageSetupDialog1.PageSettings.Landscape = False

        '预览
        If PageSetupDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized
            PrintPreviewDialog1.ShowDialog()
        End If

    End Sub

    '打印事件
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        '设置Graphics实例
        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        '缩小区域()
        'g.DrawRectangle(pen, rect)


        '标题
        Dim font As Font = New Font(New FontFamily("黑体"), 24)
        g.DrawString("绍康电器商店结算单", font, Brushes.Black, 250, 30)

        '入货情况
        font = New Font(New FontFamily("黑体"), 18)
        g.DrawString("入货情况", font, Brushes.Black, 60, 120)

        font = New Font(New FontFamily("宋体"), 14)
        g.DrawString("品牌型号：", font, Brushes.Black, 80, 170) : g.DrawString(Label19.Text, font, Brushes.Black, 180, 170)
        g.DrawString("入货数量：", font, Brushes.Black, 320, 170) : g.DrawString(Label18.Text, font, Brushes.Black, 420, 170)
        g.DrawString("入货支出：", font, Brushes.Black, 560, 170) : g.DrawString(Label20.Text, font, Brushes.Black, 660, 170)

        '出售情况
        font = New Font(New FontFamily("黑体"), 18)
        g.DrawString("出售情况", font, Brushes.Black, 60, 270)

        font = New Font(New FontFamily("宋体"), 14)
        g.DrawString("品牌型号：", font, Brushes.Black, 80, 320) : g.DrawString(Label22.Text, font, Brushes.Black, 180, 320)
        g.DrawString("出售数量：", font, Brushes.Black, 320, 320) : g.DrawString(Label21.Text, font, Brushes.Black, 420, 320)
        g.DrawString("出售收入：", font, Brushes.Black, 560, 320) : g.DrawString(Label23.Text, font, Brushes.Black, 660, 320)
        g.DrawString("待售数量：", font, Brushes.Black, 320, 370) : g.DrawString(Label24.Text, font, Brushes.Black, 420, 370)

        '结算情况
        font = New Font(New FontFamily("黑体"), 18)
        g.DrawString("结算情况", font, Brushes.Black, 60, 470)

        font = New Font(New FontFamily("宋体"), 14)
        g.DrawString("总支出：", font, Brushes.Black, 320, 520) : g.DrawString(Label27.Text, font, Brushes.Black, 400, 520)
        g.DrawString("总收入：", font, Brushes.Black, 560, 520) : g.DrawString(Label26.Text, font, Brushes.Black, 640, 520)
        g.DrawString("待售值：", font, Brushes.Black, 320, 570) : g.DrawString(Label28.Text, font, Brushes.Black, 400, 570)
        g.DrawString("暂盈利：", font, Brushes.Black, 560, 570) : g.DrawString(Label25.Text, font, Brushes.Black, 640, 570)

        '内容
        font = New Font(New FontFamily("宋体"), 12)
        g.DrawString("打印时间：" + Now.ToString, font, Brushes.Black, 570, rect.Bottom)
        g.DrawString("结算用户：" + AccountName, font, Brushes.Black, 570, rect.Bottom - 50)


        e.HasMorePages = False
        '释放资源
        font.Dispose()
        pen.Dispose()
    End Sub

End Class